package creator.ui.controller;

import creator.ui.graphics.Magicanvas;
import creator.ui.graphics.bean.Switch;
import creator.ui.graphics.polaroid.Animation;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.Toolkit;
import java.io.PrintStream;
import java.util.ArrayList;
import java.util.Iterator;

public abstract class Processor {

  public static final Dimension DIM = Toolkit
    .getDefaultToolkit()
    .getScreenSize();
  public static InternalFrom INTERNAL_FROM;
  public static ArrayList<Magicanvas> CanvasList;
  public static Magicanvas thisCanvas;
  public static int canvasSub;
  private static ArrayList<Animation> ANIMATIONS;
  private static int threadNum = 0;

  public Processor() {
    CanvasList = new ArrayList();
    INTERNAL_FROM = new InternalFrom();
    initFrame();
    loadMagicanvas();
  }

  private int getThreadNum() {
    return threadNum++;
  }

  public abstract void initFrame();

  public static void setFrame(InternalFrom frame) {
    INTERNAL_FROM = frame;
  }

  public static InternalFrom getFrame() {
    return INTERNAL_FROM;
  }

  public abstract void loadMagicanvas();

  public void addMagicanvas(Magicanvas magicanvas) {
    CanvasList.add(magicanvas);
  }

  public void start() {
    run();
  }

  private Magicanvas getNewCanvas(int i) {
    CanvasList.clear();
    loadMagicanvas();
    return (Magicanvas) CanvasList.get(i);
  }

  private void run() {
    try {
      while (Switch._getSub() < CanvasList.size()) {
        checkJump();
        thisCanvas = getNewCanvas(Switch._getSub());
        INTERNAL_FROM.getContentPane().add(thisCanvas);
        INTERNAL_FROM.setVisible(true);
        ANIMATIONS = thisCanvas.getAnimations();
        Switch._openContinue();
        Animation animation;
        for (
          Iterator localIterator = ANIMATIONS.iterator();
          localIterator.hasNext();
          animation.start()
        ) animation = (Animation) localIterator.next();
        Animation animation;
        for (
          localIterator = ANIMATIONS.iterator();
          localIterator.hasNext();
          animation.join()
        ) animation = (Animation) localIterator.next();
        INTERNAL_FROM.getContentPane().remove(thisCanvas);

        Switch._setSub(Switch._getSub() + 1);
      }
    } catch (InterruptedException e) {
      e.printStackTrace();
    }
  }

  private void checkJump() {
    new Thread(
      new Runnable() {
        public void run() {
          int thisNum = Processor.this.getThreadNum();
          System.out.println("-> Testing whether jump. t-" + thisNum);
          Switch._openTestSwitch();
          while (Switch._isTesting()) {
            if (Switch._isJump()) {
              System.out.println("-> Jump to " + Switch._getSub());
              Switch._closeJump();
              break;
            }
          }
          System.out.println("-> Test over. t-" + thisNum);
        }
      }
    )
      .start();
  }

  public static Dimension getDimension() {
    return DIM;
  }
}
